
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import axios from 'axios'





import echarts from 'echarts';
Vue.prototype.$echarts = echarts;

import {uuid} from "@/utils";

import VueAxios from 'vue-axios'
Vue.use(VueAxios, axios)
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import store from "@/store";
Vue.use(ElementUI);






// Add a request interceptor
axios.interceptors.request.use(function (config) {
    // Do something before request is sent
    config.headers.token=sessionStorage.getItem("token")

    return config;
}, function (error) {
    // Do something with request error
    return Promise.reject(error);
});


let uid=uuid(); //获取uuid
if(sessionStorage.getItem('uid')==null){

    sessionStorage.setItem('uid',uid);
}


Vue.config.productionTip = false

axios.defaults.baseURL="http://127.0.0.1:8100";




router.beforeEach((to, from, next) => {
    let isAuthenticated = sessionStorage.getItem("token");
    if (to.name !== 'login' && !isAuthenticated) next({ name: 'login' })
    if(to.name !== 'login' && isAuthenticated && router.getRoutes().length==2){
        axios.get("http://localhost:8100/system/system/list").then(res=>{
            let menus=res.data.data;
            menus.forEach(menu1=>{
                // eslint-disable-next-line no-prototype-builtins
                if (menu1.hasOwnProperty("children")) {
                    menu1.children.forEach(menu2 => {
                        let routerChild = {
                            path: menu2.path,
                            name: menu2.name,
                            component: () => import('./views/' + menu2.component + '.vue')
                        }
                        router.options.routes[0].children.push(routerChild)
                    })
                }
            })
            router.addRoutes(router.options.routes)
        })
    }
    next()
})






//})
// axios.defaults.baseURL="http://192.168.200.134:8888/";
new Vue({
    router,
    store,
    render: h => h(App)
}).$mount('#app')
